package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapping;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private ShoppingCartMapping shoppingCartMapping;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void addCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        // 1、判断该商品是否已经存在购物车——条件：userId、dishId、dishFlavor
        // 只查当前用户自己的
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart cart = shoppingCartMapping.selectBy(shoppingCart);
        if (cart == null) {
            // 代表购物车不存在该商品，新增
            // 判断是新增套餐还是菜品
            if (shoppingCartDTO.getDishId() != null) {
                // 新增菜品
                // 1、查询菜品信息
                Dish dish = dishMapper.getDishById(shoppingCartDTO.getDishId());
                // 2、保存到购物车
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                Setmeal setmeal = setmealMapper.selectByPrimaryKey(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1); // 数量--》到底是1还是加1？判断该商品是否已经存在购物车
            shoppingCart.setCreateTime(LocalDateTime.now());

            // 3、将商品存入购物车
            shoppingCartMapping.insert(shoppingCart);
        } else {
            // 代表购物车有该商品，数量+1
            // 4、更新购物车数量
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapping.update(cart);
        }
    }

    @Override
    public List<ShoppingCart> list() {
        return shoppingCartMapping.list(BaseContext.getCurrentId());
    }

    @Override
    public void clean() {
        shoppingCartMapping.clean(BaseContext.getCurrentId());
    }

    @Override
    public void subCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        // 1、查看购物车，判断该商品的数量是否大于1
        ShoppingCart cart = shoppingCartMapping.selectBy(shoppingCart);
        if (cart.getNumber() > 1) {
            // 2、数量-1
            cart.setNumber(cart.getNumber() - 1);
            shoppingCartMapping.update(cart);
        } else {
            // 3、数量=0，删除该商品
            shoppingCartMapping.delete(cart);
        }
    }
}
